package com.lecture.view.bean;

import java.util.List;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import com.lecture.hibernate.pojo.Account;
import com.lecture.hibernate.pojo.Optionsubject;
import com.lecture.hibernate.pojo.Teacher;
import com.lecture.service.OptionsubjectService;
import com.lecture.view.util.MessageFactory;
import com.lecture.hibernate.pojo.StudentSubject;
import javax.faces.model.SelectItem;

public class SubjectSelectBean extends BaseBean {
	private List<Optionsubject> optionsubjects;
	private DataModel optionsubjectsModel;
	private OptionsubjectService optionsubjectService;
	private Account account;
	private int osId;// 0 is not teacher,1 is teacher;
	private List<SelectItem> selectedList;
	public List getSelectedList()
	{
		return this.selectedList;
	}
	public void setSelectedList(List select)
	{
		this.selectedList = select;
	}
	public OptionsubjectService getOptionsubjectService() {
		return this.optionsubjectService;
	}

	public void setOptionsubjectService(OptionsubjectService service) {
		this.optionsubjectService = service;
	}

	public Account getAccount() {
		return this.account;
	}

	public void setAccount(Account logonAccount) {
		this.account = logonAccount;
	}

	public int getOsId() {
		return this.osId;
	}

	public void setOsId(int id) {
		this.osId = id;
	}

	public void setOptionsubjects(List<Optionsubject> optionsubjects) {
		this.optionsubjects = optionsubjects;
	}

	public List<Optionsubject> getOptionsubjects() {
		try {
			optionsubjects = optionsubjectService.findOptionsubject();
			return optionsubjects;
		} catch (Exception e) {
			return null;
		}

	}

	public DataModel getOptionsubjectsModel()throws Exception {

		Teacher teacher = this.account.getTeacher();
		optionsubjects = optionsubjectService.findOSByTeacher(teacher);
		selectedList = new ArrayList();
		if(optionsubjects!=null)
		{
			for(int i=0;i<optionsubjects.size();i++)
			{
				SelectItem si = new SelectItem(optionsubjects.get(i).getOsId(),"");
				selectedList.add(si);
			}
		}
		optionsubjectsModel = new ListDataModel();
		optionsubjectsModel.setWrappedData(optionsubjects);
		return optionsubjectsModel;
	}

	public void setOptionsubjectsModel(DataModel optionsubjectsModel) {
		this.optionsubjectsModel = optionsubjectsModel;
	}
	public String saveOptionSubject() {
		try {
			StudentSubject studentSubject = new StudentSubject();
			studentSubject.setOsId(osId);
			studentSubject.setStudent(account.getStudent());
			optionsubjectService.saveOptionSubject(studentSubject);
			FacesMessage fmessage = MessageFactory.getMessage(this.context,
					"page.stu_bxtk.success");
			context.addMessage(null, fmessage);
			return NavigationResults.SUCCESS;
		} catch (Exception e) {
			FacesMessage fmessage = MessageFactory.getMessage(this.context,
					"page.stu_bxtk.failure");
			context.addMessage(null, fmessage);
			return NavigationResults.FAILURE;
		}

	}
}
